<template>
  <!-- 货物入仓详情修改 -->
  <el-dialog title="货物明细" :visible.sync="openWarehouseInDetailEdit" width="800px" append-to-body :destroy-on-close="true" :before-close="beforeClose">
    <el-form ref="formParams" :model="formParams" :rules="rules" label-width="120px" :inline="true">
      <el-row>
        <el-col :span="12">
          <el-form-item label="客户条码" prop="sptm">
            <el-input
            v-model="formParams.sptm"
            :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库" prop="spckmc">
              <el-select
                v-model="formParams.spckmc"
                placeholder="请选择仓库"
                clearable
                @change="changeWarehouse"
                filterable 
                :loading="selectWarehouse7Loading"
                :disabled="true"
              >
                <el-option
                    v-for="item in warehouseList"
                    :key="item.spckid"
                    :label="item.spckmc"
                    :value="item.spckid"
                />
            </el-select>
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="物品规格" prop="spzs">
            <el-input
            v-model="formParams.spzs"
            placeholder="请输入物品规格"
            clearable
            :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓位" prop="ckcw">
             <el-select
                v-model="formParams.ckcw"
                @change="changePosition"
                placeholder="请选择仓位"
                clearable
                filterable
                :loading="warehouseInSelectPositionLoading"
                :disabled="true"
              >
                <el-option
                    v-for="item in warehouseSelectPositonList"
                    :key="item.liid"
                    :label="item.ckcw"
                    :value="item.liid"
                />
            </el-select>
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="件数" prop="spjs">
            <el-input
            v-model="formParams.spjs"
            placeholder="请输入件数"
            type="number"
            clearable
            min="0"
            :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品牌" prop="spcd">
            <el-input
            v-model="formParams.spcd"
            placeholder="请输入品牌"
            clearable
            :disabled="true"
            />
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="单重(KG)" prop="spjsl">
            <el-input
            v-model="formParams.spjsl"
            placeholder="请输入单重(KG)"
            type="number"
            clearable
            min="0"
            :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="柜号" prop="spgh">
            <el-input
            v-model="formParams.spgh"
            placeholder="请输入柜号"
            clearable
            :disabled="true"
            />
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="重量(吨)" prop="spsl">
            <el-input
            v-model="formParams.spsl"
            :value="getWeight"
            :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批号" prop="spbh">
            <el-input
            v-model="formParams.spbh"
             placeholder="请输入批号"
            clearable
            :disabled="true"
            />
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
           <el-form-item label="备注" prop="itbz">
            <el-input
              v-model="formParams.itbz"
              placeholder="请输入备注"
              clearable
              :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纱支" prop="wpggA" :rules="rules.wpggA">
            <el-select
                v-model="formParams.wpggA"
                placeholder="请选择纱支"
                clearable
                filterable
                :loading="warehouseInDetailsDropDown1Loading"
                :disabled="true"
              >
                <el-option
                    v-for="item in warehouseInDetailsList.a"
                    :key="item.id"
                    :label="item.name"
                    :value="item.name"
                />
            </el-select>
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="产地" prop="wpggE">
            <el-select
                v-model="formParams.wpggE"
                placeholder="请选择产地"
                clearable
                filterable
                :loading="warehouseInDetailsDropDown1Loading"
                :disabled="true"
              >
                <el-option
                    v-for="item in warehouseInDetailsList.e"
                    :key="item.id"
                    :label="item.name"
                    :value="item.name"
                />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工艺" prop="wpggB">
            <el-select
                v-model="formParams.wpggB"
                placeholder="请选择工艺"
                clearable
                filterable
                :loading="warehouseInDetailsDropDown1Loading"
                :disabled="true"
              >
                <el-option
                    v-for="item in warehouseInDetailsList.b"
                    :key="item.id"
                    :label="item.name"
                    :value="item.name"
                />
            </el-select>
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="漂染性" prop="wpggF">
             <el-select
                v-model="formParams.wpggF"
                placeholder="请选择漂染性"
                clearable
                filterable
                :loading="warehouseInDetailsDropDown1Loading"
                :disabled="true"
              >
                <el-option
                    v-for="item in warehouseInDetailsList.f"
                    :key="item.id"
                    :label="item.name"
                    :value="item.name"
                />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工序" prop="wpggC">
             <el-select
                v-model="formParams.wpggC"
                placeholder="请选择工序"
                clearable
                filterable
                :loading="warehouseInDetailsDropDown1Loading"
                :disabled="true"
              >
                <el-option
                    v-for="item in warehouseInDetailsList.c"
                    :key="item.id"
                    :label="item.name"
                    :value="item.name"
                />
            </el-select>
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="生产日期" prop="wpggG">
              <el-input
                v-model="formParams.wpggG"
                placeholder="请输入生产日期"
                clearable
                :disabled="true"
              />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用途" prop="wpggD">
            <el-select
                v-model="formParams.wpggD"
                placeholder="请选择用途"
                clearable
                filterable
                :loading="warehouseInDetailsDropDown1Loading"
                :disabled="true"
              >
                <el-option
                    v-for="item in warehouseInDetailsList.d"
                    :key="item.id"
                    :label="item.name"
                    :value="item.name"
                />
            </el-select>
        </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="颜色" prop="wpggH">
            <el-input
            v-model="formParams.wpggH"
            placeholder="请输入颜色"
            clearable
            :disabled="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入仓日期" prop="ssrqid">
            <el-date-picker
                v-model="formParams.ssrqid"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="请输入入仓日期"
                :disabled="true"
              >
            </el-date-picker>
        </el-form-item>
        </el-col>
      </el-row>
       <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="拆分件数" prop="spjs3">
            <el-input
              v-model="formParams.spjs3"
              placeholder="请输入拆分件数"
              type="number"
              clearable
              min="0"
              @input="changeSpjs3"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="拆分重量" prop="splitWeight">
            <el-input
            v-model="splitWeight"
            :value="getSplitWeight"
            :disabled="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
       <el-row>
        <el-col :span="12">
          <el-form-item label="拆分入仓日期" prop="ssrqid3">
            <el-date-picker
              v-model="formParams.ssrqid3"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请输入拆分入仓日期"
              :disabled="!formParams.spjs3">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="新仓位" prop="ckcw3">
             <el-select
                v-model="formParams.ckcw3"
                @change="changeNewPosition"
                placeholder="请选择新仓位"
                clearable
                filterable
                :disabled="!formParams.spjs3"
              >
                <el-option
                    v-for="item in warehouseSelectPositonList"
                    :key="item.liid"
                    :label="item.ckcw"
                    :value="item.liid"
                />
            </el-select>
        </el-form-item>
        </el-col>
      </el-row> -->
    </el-form>
    <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEdit('formParams')" v-hasPermi="['zhcloud:hwrc:modify']">修 改</el-button>
      </div> -->
  </el-dialog>
</template>

<script>
import { warehouseInfModify, warehouseInDetailsDropDown1, warehouseInSelectPosition } from "@/api/storage/commonGoods/warehouseIn";
import { selectWarehouse7 } from "@/api/storage/warehouse";
//import { formatDate } from '@/utils'
import { NUMBER_DIDGIT, UNIT_WEIGHT_DIGIT, WEIGHT_DIGIT } from '@/utils/constant'


export default {
  name: "CWarehouseInDetailEdit",
  data() {
    return {
      //仓库加载
      selectWarehouse7Loading:true,
      //仓位加载
      warehouseInSelectPositionLoading: true,
      //纱织加载
      warehouseInDetailsDropDown1Loading: true,
       // 表单校验
      rules: {
        spzs: [
          { required: true, message: "物品规格不能为空", trigger: "blur" }
        ],
        wpggA: [
          { required: true, message: "纱支不能为空", trigger: "blur" }
        ],
        spjs: [
          { required: true, message: "件数不能为空", trigger: "blur" }
        ],
        spjsl: [
          { required: true, message: "单重不能为空", trigger: "blur" }
        ],
        ssrqid3: [
          { required: false, message: "拆分入仓日期不能为空", trigger: "blur" }
        ],
        ckcw3: [
          { required: false, message: "新仓位不能为空", trigger: "blur" }
        ],
        // spjs3: [
        //   { required: true, message: "拆分件数不能为空", trigger: "blur" },
        //   { required: true, validator: this.checkspjs3, trigger: "blur" }
        // ]
      },
      //弹窗状态
      openWarehouseInDetailEdit: false,
      //拆分重量
      splitWeight:0,
      //表单数据
      formParams:{},
      submitParams: {
        dzid: "",
        itbz: "",
        khmc: "",
        khmcid: "",
        spbh: "",
        spbhid: "",
        spcd: "",
        spckid: "",
        spckid1: "",
        spcwid: "",
        spcwid1: "",
        spcwid3: "",
        spdzld: "",
        spgh: "",
        spjs: "",
        spjs3: "",
        spjsl: "",
        spmc: "",
        spsl: "",
        spzs: "",
        ssrqid: "",
        ssrqid3: "",
        wbdh: "",
        wpggA: "",
        wpggB: "",
        wpggC: "",
        wpggD: "",
        wpggE: "",
        wpggF: "",
        wpggG: "",
        wpggH: ""
      },
      //选择仓库入参
      selectPositionQuery:{
        spckid:""
      },
      warehouseInDetailsList: [],
      warehouseList:[],
      warehouseSelectPositonList:[],
    };
  },
  computed: {
    getSplitWeight() {
      const spjs3 = parseFloat(this.formParams.spjs3)
      const spjsl = parseFloat(this.formParams.spjsl)
      if(spjs3 || spjs3 == 0) {
        this.splitWeight = parseFloat((spjs3*spjsl/1000).toFixed(WEIGHT_DIGIT))
      }
    },
    getWeight() {
      if(!this.formParams.spjs) this.formParams.spjs = 0
      if(!this.formParams.spjsl) this.formParams.spjsl = 0
      const spjs = parseFloat(this.formParams.spjs)
      const spjsl = parseFloat(this.formParams.spjsl)
      if(spjs || spjs == 0) {
        this.formParams.spsl = parseFloat((spjs*spjsl/1000).toFixed(WEIGHT_DIGIT))
      }
    },
  },
  watch:{
    // 'formParams.spjs': function(data) {
    //   if(data !== undefined) {
    //     if(data.indexOf('.')>0){
    //       data=data.slice(0,data.indexOf('.')+ (NUMBER_DIDGIT + 1))
    //     }
    //     this.formParams.spjs = data
    //   }
    // },
    // 'formParams.spjsl': function(data) {
    //   if(data !== undefined) {
    //      if(data.indexOf('.')>0){
    //       data=data.slice(0,data.indexOf('.')+ (UNIT_WEIGHT_DIGIT + 1))
    //       }
    //       this.formParams.spjsl = data
    //     }
    // },
    'formParams.spjs3': function(data) {
      if(data !== undefined) {
        if(data.indexOf('.')>0){
          data=data.slice(0,data.indexOf('.')+ (NUMBER_DIDGIT + 1))
        }
        this.formParams.spjs3 = data
      }
      
    }
  },
  methods: {
     /**获取仓库 */
    getWarehouse() {
      this.selectWarehouse7Loading = true
      selectWarehouse7({}).then(response => {
        this.warehouseList = response.data
        this.selectWarehouse7Loading = false
      });
    },
    /**选择仓位 */
    selectPosition() {
      this.warehouseInSelectPositionLoading = true
      warehouseInSelectPosition(this.selectPositionQuery).then(response => {
        this.warehouseSelectPositonList = response.data
        this.warehouseInSelectPositionLoading = false
      });
    },
     /**选择纱织 */
    warehouseInDetails() {
      this.warehouseInDetailsDropDown1Loading = true
      warehouseInDetailsDropDown1({}).then(response => {
        this.warehouseInDetailsList = response.data;
        this.warehouseInDetailsDropDown1Loading = false
      });
    },
    /**修改 */
    handleWarehouseInfModify() {
      this.$modal.loading("数据正在修改中，请稍等...")
      warehouseInfModify(this.submitParams).then(response => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('修改成功！')
        this.close()
        this.$emit('refresh', true)
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },
    //选择仓库
    changeWarehouse(data) {
        this.formParams.spckid = data
        this.selectPositionQuery.spckid = data
        this.selectPosition()
    },
    //选择仓位
    changePosition(data) {
      this.formParams.spcwid = data
    },
    //选择新仓位
    changeNewPosition(data ) {
      this.formParams.spcwid3 = data
    },
    //改变拆分件数
    changeSpjs3(data) {
      if(data) {
        this.rules.ssrqid3[0].required = true
        this.rules.ckcw3[0].required = true
      } else {
        this.rules.ssrqid3[0].required = false
        this.rules.ckcw3[0].required = false
        this.$set(this.formParams,'ssrqid3', "")
        this.$refs.formParams.clearValidate()
      }
       
    },
    /**验证拆分件数 */
    // checkspjs3(rule, value, callback) {
    //   const spjs = parseFloat(this.formParams.spjs)
    //   if(spjs < value) {
    //     callback("拆分件数不可超过件数")
    //   } else {
    //     callback()
    //   }
    // },
    //修改详情
    handleEdit(formName) {
      this.$refs[formName].validate((valid, data) => {
        if(valid) {
          this.$modal.confirm("是否修改？").then(()=> {
            const submitParams = this.submitParams
            const formParams = this.formParams
            for(let i in submitParams) {
              submitParams[i] = formParams[i]
            }
            this.handleWarehouseInfModify()
          }).catch(() => {})
        } else {
          for(let i in data) {
            const item = data[i]
            for(let j in item) {
              const message = item[j].message
              return this.$modal.msgError(message)
            }
          }
          return false
        }
      })
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
    },
    /**关闭弹窗 */
    beforeClose(done) {
      this.close()
      done()
    },
    /**关闭窗口 */
    close() {
      this.changeWarehouseInDetailEdit()
      this.reset()
    },
    /**改变窗口状态 */
    changeWarehouseInDetailEdit() {
      this.openWarehouseInDetailEdit = !this.openWarehouseInDetailEdit;
    },

    /**打开弹窗 */
    open(data, dzid) {
      //const nowDate = formatDate(Date.now()).split(" ")[0]
      this.changeWarehouseInDetailEdit()
      this.selectPositionQuery.spckid = data.spckid
      this.formParams = data
      this.formParams.dzid = dzid
      this.formParams.khmc = data.spkhmc
      this.formParams.khmcid = data.spkhmcid
      this.formParams.spckid1 = data.spckid
      this.formParams.spcwid1 = data.spcwid
      this.formParams.spcwid3 = data.spcwid
      this.formParams.itbz = data.spbz
      //this.$set(this.formParams, "ssrqid3", nowDate)
      this.$set(this.formParams, "ckcw3", data.ckcw)
      // this.getWarehouse()
      // this.warehouseInDetails()
      // this.selectPosition()
    },
  }
};
</script>
